package com.dgut.smart_house.service.impl;

import com.dgut.smart_house.domain.User;
import com.dgut.smart_house.mapper.UserMapper;
import com.dgut.smart_house.service.UserService;
import com.dgut.smart_house.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserId(String userId) {
        return userMapper.findByUserId(userId);
    }

    @Override
    public int createUser(String userId, String password, String email) {
        //生产随机加密盐
        int saltseed = (int) ((Math.random() * (99999 - 10000 + 1)) + 10000);
        String salt = String.valueOf(saltseed);
        //创建User对象
        User user = new User();
        user.setEmail(email);
        user.setSalt(salt);
        user.setPassword(Md5Util.Md5tool(password, salt));
        user.setUserId(userId);
        user.setPerms("user:ordinary");
        //创建DB账户
        return userMapper.createUser(user);

    }
}
